package com.test.httpClient;

import java.io.IOException;

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.cookie.CookieSpec;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class SimpleClient {

	/**
	 * @param args
	 * @throws IOException
	 * @throws HttpException
	 */
	public static void main(String[] args) throws HttpException, IOException {
		 getMethod();
		// postMethod();
		// login();
	}

	private static void getMethod() throws IOException, HttpException {
		// 构造HttpClient 实例
		HttpClient client = new HttpClient();
//		client.getState().setCredentials( "www.verisign.com" , "realm" , new UsernamePasswordCredentials( "username" , "password" ) );
		// 创建GET方法实例
		HttpMethod method = new GetMethod("https://www.verisign.com/products-services/index.html");
//		method.setDoAuthentication( true );
		// 使用系统提供的默认的恢复策略
		method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
				new DefaultHttpMethodRetryHandler());
		// 执行method方法
		int statusCode = client.executeMethod(method);
		if (statusCode != HttpStatus.SC_OK) {
			System.err.println("Method failed: " + method.getStatusLine());
		}
		System.out.println(method.getResponseBodyAsString());
		method.releaseConnection();
	}

	private static void postMethod() throws HttpException, IOException {
		// 构造HttpClient 实例
		HttpClient client = new HttpClient();
		// 创建post方法
		PostMethod method = new PostMethod(
				"http://www.imobile.com.cn/simcard.php");
		// 填入各个表单域的值
		NameValuePair data[] = { new NameValuePair("mob", "15000408") };
		// 将表单的值放入postMethod中
		method.setRequestBody(data);

		int statusCode = client.executeMethod(method);
		if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY
				|| statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
			Header locationHeader = method.getResponseHeader("location");
			// 从头中取出转向的地址
			String location = null;
			if (locationHeader != null) {
				location = locationHeader.getValue();
				System.out.println("location......." + location);
				method.releaseConnection();
				// 重定向
				GetMethod redirect = new GetMethod(location);
				client.executeMethod(redirect);
				System.out.println(redirect.getResponseBodyAsString());
				System.out.println("Redirect:"
						+ redirect.getStatusLine().toString());
				redirect.releaseConnection();
				//
			}
		}
	}

	// 模拟登录
	private static void login() throws HttpException, IOException {
		String LOGIN_SITE = "localhost";
		int LOGIN_PORT = 8089;
		HttpClient client = new HttpClient();
		client.getHostConfiguration().setHost(LOGIN_SITE, LOGIN_PORT, "http");
		
		// 先登录
		PostMethod postMethod = new PostMethod("/test_01/login");
		NameValuePair data[] = { new NameValuePair("userName", "x_hwang"),
				new NameValuePair("passwd", "") };
		postMethod.setRequestBody(data);
		client.executeMethod(postMethod);
		System.out.println(new String(postMethod.getResponseBodyAsString().getBytes("ISO-8859-1")));
		postMethod.releaseConnection();
		
		// 查看cookie信息
		CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
		Cookie[] cookies = cookiespec.match(LOGIN_SITE, LOGIN_PORT, "/", false,
				client.getState().getCookies());
		System.out.println(cookies.length);

		// 再访问其它的页面
		GetMethod getMethod = new GetMethod("/test_01/index.jsp");
		client.executeMethod(getMethod);
		System.out.println(new String(getMethod.getResponseBodyAsString().getBytes("UTF-8")));
		getMethod.releaseConnection();
	}
}
/**
* 多线程可以这样new client
* MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
* HttpClient client = new HttpClient(connectionManager);
**/